/**
 * author wen
 * cunyu
 * email wjd_13@hotmail.com
 */
package com.cunyu.action;

import cn.tom.kit.StringUtil;
import com.cunyu.bean.Muser;
import com.cunyu.bean.OrderPool;
import com.cunyu.bean.ReqData;
import com.cunyu.bean.RespData;
import com.cunyu.service.CityService;
import com.cunyu.service.MuserService;
import com.cunyu.service.OrderPoolService;
import com.cunyu.service.TagService;
import com.cunyu.util.BaseUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 信贷客户池管理
 * @author win
 * 2019/04/19-14:37
 */
@Controller
public class OrderPoolController extends BaseController{

    private Logger logger = LoggerFactory.getLogger(OrderPoolController.class);

    @Resource
    CityService cityService ;

    @Resource
    MuserService muserService ;

    @Resource
    OrderPoolService  orderPoolService ;

    @Resource
    TagService tagService ;

    // 视图跳转
    @RequestMapping("/channel")
    public String channel(HttpServletResponse res,Model model,String qcode){
        if(!StringUtil.hasLength(qcode)){
            printHTML(res,"404");
            return null ;
        }
        // has qCode ?
        int muid = muserService.hasQcode(qcode)  ;
        if(muid == 0){
            printHTML(res,"404:/"+qcode);
            return null ;
        }

        Map<String, Map<String, Object>> tagInfo = tagService.getTagInfo();
        List<Map<String, Object>> mapList = cityService.provinceList();

        model.addAttribute("qcode",qcode) ;
        model.addAttribute("tagInfo",tagInfo) ;
        model.addAttribute("provList",mapList) ;
        return "channel" ;
    }

    @RequestMapping("/orderpool/list")
    public String list(Model model , ReqData reqData, OrderPool pool){
        Muser user = getOnlineUser();
        RespData<List<Map<String, Object>>> orders = orderPoolService.getOrders(reqData, pool,user);
        Map<String, Map<String, Object>> tagInfo = tagService.getTagInfo();
        model.addAttribute("respData",orders) ;
        model.addAttribute("tagInfo",tagInfo) ;
        model.addAttribute("form",pool) ;
        return "orderpool/list" ;
    }

    @RequestMapping("/orderpool/info")
    public String info(Model model , String id){
        if(StringUtil.hasLength(id)){
            Map<String, Object> order = orderPoolService.getOrder(Integer.valueOf(id));
            model.addAttribute("respData",order) ;

            Object pcode = order.get("pcode");
            if(pcode != null && !"0".equals(pcode)){
                List<Map<String, Object>> cityList = cityService.cityList(pcode.toString());
                model.addAttribute("cityList",cityList) ;
            }
        }
        Map<String, Map<String, Object>> tagInfo = tagService.getTagInfo();
        List<Map<String, Object>> mapList = cityService.provinceList();
        model.addAttribute("tagInfo",tagInfo) ;
        model.addAttribute("provList",mapList) ;
        model.addAttribute("today", BaseUtil.getTodayStr()+" 00:00:00") ;
        return "orderpool/info" ;
    }

    @RequestMapping("/orderpool/show")
    public String show(Model model , String id){
        Map<String, Object> order = orderPoolService.getOrder(Integer.valueOf(id));
        model.addAttribute("respData",order) ;
        Map<String, Map<String, Object>> tagInfo = tagService.getTagInfo();
        model.addAttribute("tagInfo",tagInfo) ;
        return "orderpool/show";
    }

    // ajax request or other redirect
    @RequestMapping("/orderpool/sourceList")
    public void sourceList(HttpServletResponse res, String source){
        List<Map<String, Object>> nameList = null ;
        if("0".equals(source)){
            nameList = muserService.getNameList();
        }else if("1".equals(source)){
            nameList = new ArrayList<>() ;
        }else{
            nameList = new ArrayList<>() ;
        }
        printOK(res,"success",nameList);
    }

    @RequestMapping("/orderpool/dataUpdate")
    public void dataUpdate(HttpServletResponse response, OrderPool pool,String id) throws IOException {
        int x ;
        if(StringUtil.hasLength(id)){
            x = Integer.valueOf(id);
            orderPoolService.updatePool(pool,x) ;
        }else {
            // 新建单都不打折？
            pool.setDiscount(10);
            x = orderPoolService.newPool(pool) ;
        }
        response.sendRedirect("/orderpool/list?change="+x);
    }

    @RequestMapping("/orderpool/verify")
    public void verify(HttpServletResponse response,String id) throws IOException {
        orderPoolService.verfiy(Integer.valueOf(id)) ;
        response.sendRedirect("/orderpool/show?id="+id);
    }

    @RequestMapping("/channelInput")
    public void channelInput(HttpServletResponse response, OrderPool pool) {
        pool.setSource(2);
        pool.setDiscount(10);
        if(pool.getCitycode() == null || pool.getCitycode() == 0){
            try{
                orderPoolService.setCityCode(pool) ;
            }catch (Exception e){
                printHTML(response,"录入手机号重复 <a href='/channel?qcode="+
                        pool.getQcode()
                        +"'> 继续添加</a>");
            }
        }
        int x = orderPoolService.newPool(pool) ;
        if(x == -1){
            printHTML(response,"录入手机号重复 <a href='/channel?qcode="+
                    pool.getQcode()
                    +"'> 继续添加</a>");
        }else {
            printHTML(response,"录入数据成功 <a href='/channel?qcode="+
                    pool.getQcode()
                    +"'> 继续添加</a>");
        }
    }

    @RequestMapping("/channelIn")
    public void channelIn(HttpServletResponse response, OrderPool pool) {
        try{

            int a = orderPoolService.checkQcode(pool) ;
            if(a == 0){
                printError(response,"无效的渠道码");
                return;
            }
            orderPoolService.setCityCode(pool) ;

            if(pool.getCitycode() ==  null || pool.getCitycode() == 0){
                printError(response,"不能识别的城市");
                return;
            }
        }catch (Exception e){
            printError(response,"不能识别的城市");
            logger.error("",e);
            return;
        }
        pool.setDiscount(10);
        pool.setSource(2);
        try{
            int x = orderPoolService.newPool(pool) ;
            if(x == -1){
                printError(response,"录入手机号重复");
            }else {
                printOK(response,"录入数据成功");
            }
        }catch (Exception e){
            printError(response,"数据异常");
            logger.error("",e);
        }
    }

}
